package ${groupId}.service.security.filter;

import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.system.core.view.common.response.CommonResponse;

public class MyLogoutFilter extends LogoutFilter {

	@Override
	protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
		Subject subject = getSubject(request, response);
        try {
            subject.logout();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.preHandle(request, response);
	}

	@Override
	protected void issueRedirect(ServletRequest request, ServletResponse response, String redirectUrl)
			throws Exception {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        if (StringUtils.hasText(redirectUrl)) {
            httpServletResponse.setContentType("application/json");
            httpServletResponse.getWriter().write(JSONObject.toJSONString(CommonResponse.buildSuccessResult()));
        } else {
            super.issueRedirect(request, response, redirectUrl);
        }
	}

}
